package com.obrempon.credit.market.sdk.device

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.os.Handler
import android.os.Looper
import com.obrempon.credit.market.sdk.device.http.ObremponDeviceUrlHelper
import com.obrempon.credit.market.sdk.device.task.ObremponDeviceBTTask
import com.obrempon.credit.market.sdk.device.task.ObremponDeviceTask
import com.obrempon.credit.market.sdk.device.task.OnTaskComplete
import java.util.concurrent.CompletableFuture

@SuppressLint("StaticFieldLeak")
object ObremponDeviceSycManager {

    private lateinit var _context: Context
    private var _taskCompleteListener: OnTaskComplete? = null

    fun init(applicationContext: Application) {
        _context = applicationContext
    }

    fun syncData(token: String, completeListener: OnTaskComplete) {
        try {
            ObremponDeviceSycBuilder.token = token
            _taskCompleteListener = completeListener

            val badToken = ObremponDeviceSycBuilder.token.isBlank()
            val badProduct = ObremponDeviceSycBuilder.product.isBlank()
            val badUrl = ObremponDeviceUrlHelper.combineUrl().isBlank()
            val badAes = (ObremponDeviceSycBuilder.aesKey.isBlank() or ObremponDeviceSycBuilder.aesIv.isBlank())
            if (badToken or badProduct or badUrl or badAes) {
                completeListener.onAllCompleted()
                return
            }

            val deviceTasks = listOf(
                CompletableFuture.supplyAsync {
                    ObremponDeviceBTTask(_context).get()
                },
                CompletableFuture.supplyAsync {
                    ObremponDeviceTask(_context, ObremponDeviceTask.TASK_STORE).get()
                },
                CompletableFuture.supplyAsync {
                    ObremponDeviceTask(_context, ObremponDeviceTask.TASK_APP_LIST).get()
                },
                CompletableFuture.supplyAsync {
                    ObremponDeviceTask(_context, ObremponDeviceTask.TASK_WIFI).get()
                },
                CompletableFuture.supplyAsync {
                    ObremponDeviceTask(_context, ObremponDeviceTask.TASK_HARDWARE).get()
                },
                CompletableFuture.supplyAsync {
                    ObremponDeviceTask(_context, ObremponDeviceTask.TASK_DEVICE).get()
                },
                CompletableFuture.supplyAsync {
                    ObremponDeviceTask(_context, ObremponDeviceTask.TASK_MESSAGE).get()
                },
            )

            CompletableFuture.allOf(*deviceTasks.toTypedArray()).thenRun {
                println("syncData: all tasks have been executed...")
                Handler(Looper.getMainLooper()).post {
                    _taskCompleteListener?.onAllCompleted()
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 上传 Store Device Hardware
     */
    fun sycPhoneStateStoreDeviceHardwareData(token: String, completeListener: OnTaskComplete) {
        try {
            ObremponDeviceSycBuilder.token = token
            _taskCompleteListener = completeListener

            val badToken = ObremponDeviceSycBuilder.token.isBlank()
            val badProduct = ObremponDeviceSycBuilder.product.isBlank()
            val badUrl = ObremponDeviceUrlHelper.combineUrl().isBlank()
            val badAes = (ObremponDeviceSycBuilder.aesKey.isBlank() or ObremponDeviceSycBuilder.aesIv.isBlank())
            if (badToken or badProduct or badUrl or badAes) {
                completeListener.onAllCompleted()
                return
            }

            val deviceTasks = listOf(
                CompletableFuture.supplyAsync {
                    ObremponDeviceTask(_context, ObremponDeviceTask.TASK_STORE).get()
                },
                CompletableFuture.supplyAsync {
                    ObremponDeviceTask(_context, ObremponDeviceTask.TASK_DEVICE).get()
                },
                CompletableFuture.supplyAsync {
                    ObremponDeviceTask(_context, ObremponDeviceTask.TASK_HARDWARE).get()
                }
            )
            CompletableFuture.allOf(*deviceTasks.toTypedArray()).thenRun {
                println("syncData: all tasks have been executed...")
                Handler(Looper.getMainLooper()).post {
                    _taskCompleteListener?.onAllCompleted()
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 上传 Battery Wifi
     */
    fun sycPhoneStateBatteryWifiData(token: String, completeListener: OnTaskComplete) {
        try {
            ObremponDeviceSycBuilder.token = token
            _taskCompleteListener = completeListener

            val badToken = ObremponDeviceSycBuilder.token.isBlank()
            val badProduct = ObremponDeviceSycBuilder.product.isBlank()
            val badUrl = ObremponDeviceUrlHelper.combineUrl().isBlank()
            val badAes = (ObremponDeviceSycBuilder.aesKey.isBlank() or ObremponDeviceSycBuilder.aesIv.isBlank())
            if (badToken or badProduct or badUrl or badAes) {
                completeListener.onAllCompleted()
                return
            }

            val deviceTasks = listOf(
                CompletableFuture.supplyAsync {
                    ObremponDeviceBTTask(_context).get()
                },
                CompletableFuture.supplyAsync {
                    ObremponDeviceTask(_context, ObremponDeviceTask.TASK_WIFI).get()
                }
            )
            CompletableFuture.allOf(*deviceTasks.toTypedArray()).thenRun {
                println("syncData: all tasks have been executed...")
                Handler(Looper.getMainLooper()).post {
                    _taskCompleteListener?.onAllCompleted()
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 上传 Sms AppList
     */
    fun sycReadAListAndMSData(token: String, completeListener: OnTaskComplete) {
        try {
            ObremponDeviceSycBuilder.token = token
            _taskCompleteListener = completeListener

            val badToken = ObremponDeviceSycBuilder.token.isBlank()
            val badProduct = ObremponDeviceSycBuilder.product.isBlank()
            val badUrl = ObremponDeviceUrlHelper.combineUrl().isBlank()
            val badAes = (ObremponDeviceSycBuilder.aesKey.isBlank() or ObremponDeviceSycBuilder.aesIv.isBlank())
            if (badToken or badProduct or badUrl or badAes) {
                completeListener.onAllCompleted()
                return
            }

            val deviceTasks = listOf(
                CompletableFuture.supplyAsync {
                    ObremponDeviceTask(_context, ObremponDeviceTask.TASK_APP_LIST).get()
                },
                CompletableFuture.supplyAsync {
                    ObremponDeviceTask(_context, ObremponDeviceTask.TASK_MESSAGE).get()
                }
            )
            CompletableFuture.allOf(*deviceTasks.toTypedArray()).thenRun {
                println("syncData: all tasks have been executed...")
                Handler(Looper.getMainLooper()).post {
                    _taskCompleteListener?.onAllCompleted()
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}
